package demo2;

public class MyLinkList implements ILinkLIst{//双链表
    static class ListNode {
        int val;
        public ListNode next;
        public ListNode prev;
        public ListNode(int val) {
            this.val = val;
        }
    }
    private ListNode head;//链表头
    private ListNode last;//链表尾
    
    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if(head == null) {
            head = node;
            last = node;
        }else{
            node.next = head;
            head.prev = node;
            head = node;
        }
    }

    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if(head == null) {
            head = node;
            last = node;
        }else{
            last.next = node;
            node.prev = last;
            last = node;
        }
    }

    @Override
    public void addIndex(int data, int index) {
        if(index < 0 || index > size()) {
            System.out.println("位置不合法！！！");
            return;
        }
        if(index == 0) {
            addFirst(data);
            return;
        }
        if(index == size()) {
            addLast(data);
            return;
        }
        ListNode node = new ListNode(data);
        ListNode cur=search(index);
        node.next = cur;
        cur.prev.next = node;
        node.prev=cur.prev;
        cur.prev=node;
    }
    private ListNode search(int index) {
        ListNode node = head;
        while(index!=0){
            node = node.next;
            index--;
        }
        return node;
    }

    @Override
    public boolean contains(int key) {
        if(head == null) {
            return false;
        }
        ListNode cur = head;
        while(cur != null) {
            if(cur.val==key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        ListNode cur=head;
        while(cur!=null){
            if(cur.val==key){
                if(cur==head){
                    head=head.next;
                    head.prev=null;
                }else{
                    if(cur.next==null){
                        cur.prev.next=cur.next;
                        last=last.prev;
                    }else{
                        cur.prev.next = cur.next;
                        cur.next.prev = cur.prev;
                    }
                }
                return;
            }
            cur=cur.next;
        }
    }

    @Override
    public void removeAllKey(int key) {
        ListNode cur=head;
        while(cur!=null){
            if(cur.val==key){
                if(cur==head){
                    head=head.next;
                    head.prev=null;
                }else{
                    if(cur.next==null){
                        cur.prev.next=cur.next;
                        last=last.prev;
                    }else{
                        cur.prev.next = cur.next;
                        cur.next.prev = cur.prev;
                    }
                }
            }
            cur=cur.next;
        }
    }

    @Override
    public int size() {
        ListNode cur = head;
        int count=0;
        while(cur != null) {
            cur = cur.next;
            count++;
        }
        return count;
    }

    @Override
    public void display() {
        ListNode curr = head;
        while(curr != null) {
            System.out.print(curr.val+" ");
            curr = curr.next;
        }
        System.out.println();
    }

    @Override
    public void clear() {
        ListNode cur = head;
        while(cur != null) {
            ListNode curNext = cur.next;
            cur.next = null;
            cur.prev = null;
            cur = curNext;
        }
        head = null;
        last = null;
    }
}
